{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Theano 实例：线性回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在用 `theano` 进行线性回归之前，先回顾一下 `theano` 的运行模式。\n",
    "\n",
    "`theano` 是一个符号计算的数学库，一个基本的 `theano` 结构大致如下：\n",
    "\n",
    "- 定义符号变量\n",
    "- 编译用符号变量定义的函数，使它能够用这些符号进行数值计算。\n",
    "- 将函数应用到数据上去"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using gpu device 0: GeForce GTX 850M\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "import theano\n",
    "from theano import tensor as T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "简单的例子：$y = a \\times b, a, b \\in \\mathbb{R}$\n",
    "\n",
    "定义 $a, b, y$："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = T.scalar()\n",
    "b = T.scalar()\n",
    "\n",
    "y = a * b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编译函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "multiply = theano.function(inputs=[a, b], outputs=y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将函数运用到数据上："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.0\n",
      "20.0\n"
     ]
    }
   ],
   "source": [
    "print multiply(3, 2) # 6\n",
    "print multiply(4, 5) # 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "回到线性回归的模型，假设我们有这样的一组数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_X = np.linspace(-1, 1, 101)\n",
    "train_Y = 2 * train_X + 1 + np.random.randn(train_X.size) * 0.33"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分布如图："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEACAYAAACj0I2EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGGBJREFUeJzt3X2MXFd5x/HfM3G2bElLtFnqAIlIGtEGSGRtnFK3ocpU\n7e4GWrnYFgLSFLdIGNS0gmgDxqQ0GxHVpGCB0jea0MSLVMMfpaGbAjtZXsawLaUlcYILCRA1WAlg\nk2XLm2LkpPv0j5ndnZ2dmb1vM/fOme9Husq83Llzrq/zzPFzn3OOubsAAP2vlHcDAADZIKADQCAI\n6AAQCAI6AASCgA4AgSCgA0AgMgnoZnaWmR0zs3uzOB4AIL6seuhvkfQ1SRS1A0BOUgd0M7tA0isl\nfUiSpW4RACCRLHro75f0NknLGRwLAJBQqoBuZr8r6Xvufkz0zgEgV5ZmLhcz+wtJfyDpGUnPkvTz\nkj7m7q9v2Ie8OgAk4O6xOsqpeuju/k53v9DdL5b0WkmfbQzmDfsFu9188825t4Hz49w4v/C2JLKu\nQ6c3DgA52ZLVgdz9qKSjWR0PABAPI0VTKpfLeTehq0I+v5DPTeL8BlGqm6KRvsDMu/0dABAaM5P3\n8qYoAKA4COgAEAgCOgAEgoAOAIEg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4X1F/rB5uen7v/uOHxp8zsb81sxN2X1P/6+dptKu61\n63bKpWXux8x+1sx+rv742ZImJLW9i11Q7fJaX5b0IjO7yMyGJL1G0mzvmpXKrKS99cd7VesNrNOH\n1y7K9ZiV9HpJMrMdkn7QkHoquk3Pz8y2mpnVH79MtXLlEIK51N/XblOxr10X7truUi2ndVrSSUmf\nqr/+fEmfqD/+RdXuxj8o6b8lHcj7bnNW51Z//gpJX1et+qAvzq3e7hFJn1ZtOuT7JJ0bwrVrdT0k\nvUnSmxr2+ev6+w+pQ3VWEbfNzk/S9fVr9aCkf5e0I+82xzi3j0j6jqQz9f/33hDYtet4fnGvHQOL\nACAQLEEHAIEgoANAIAjoABAIAjoABIKADgCBIKADQCAI6AAQCAI6AATi/wEb9Cq85phjMgAAAABJ\nRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1b831eb8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(train_X, train_Y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义符号变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们使用线性回归的模型对其进行模拟：\n",
    "$$\\bar{y} = wx + b$$\n",
    "\n",
    "首先我们定义 $x, y$："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = T.scalar()\n",
    "Y = T.scalar()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以在定义时候直接给变量命名，也可以之后修改变量的名字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X.name = 'x'\n",
    "Y.name = 'y'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们的模型为："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def model(X, w, b):\n",
    "    return X * w + b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这里我们希望模型得到 $\\bar{y}$ 与真实的 $y$ 越接近越好，常用的平方损失函数如下：\n",
    "$$C = |\\bar{y}-y|^2$$\n",
    "\n",
    "有了损失函数，我们就可以使用梯度下降法来迭代参数 $w, b$ 的值，为此，我们将 $w$ 和 $b$ 设成共享变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "w = theano.shared(np.asarray(0., dtype=theano.config.floatX))\n",
    "w.name = 'w'\n",
    "b = theano.shared(np.asarray(0., dtype=theano.config.floatX))\n",
    "b.name = 'b'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义 $\\bar y$："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'((x * HostFromGpu(w)) + HostFromGpu(b))'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y_bar = model(X, w, b)\n",
    "\n",
    "theano.pp(Y_bar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "损失函数及其梯度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cost = T.mean(T.sqr(Y_bar - Y))\n",
    "grads = T.grad(cost=cost, wrt=[w, b])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义梯度下降规则："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lr = 0.01\n",
    "updates = [[w, w - grads[0] * lr],\n",
    "           [b, b - grads[1] * lr]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 编译训练模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每运行一次，参数 $w, b$ 的值就更新一次："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_model = theano.function(inputs=[X,Y],\n",
    "                              outputs=cost,\n",
    "                              updates=updates,\n",
    "                              allow_input_downcast=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 将训练函数应用到数据上"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练模型，迭代 100 次："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in xrange(100):\n",
    "    for x, y in zip(train_X, train_Y):\n",
    "        train_model(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "显示结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.94257426262\n",
      "1.00938093662\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEACAYAAACj0I2EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl8VOXZ//HPHQEJuGBAUXGj2pYqinFfa6oNQX8/rcDT\nl7tUlOBT3GhUQH4URK24pCq440L0ebTtU4pC1UxiNVhaV2RX3HcR5UmVKpEt1++PmUCWmcmcmTPb\nme/79ZoXk8yZM/fh6DU317mu+zgzQ0RE8l9RtgcgIiL+UEAXEQkIBXQRkYBQQBcRCQgFdBGRgFBA\nFxEJCF8CunNuG+fcIufcPD/2JyIi3vk1Q78ceANQUbuISJakHNCdc3sApwAPAC7lEYmISFL8m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eDry/EjgpWyP\n2+fjKwMp6CFOAAACfUlEQVTmZnusSR7f8UApsCzG63l77hI8Pk/nzvcZupmtNLO3E9w8ry6YJnhs\nRwDvmtmHZrYR+APwi/SPzhenATWR5zXA6XG2zZdzl8j52HLcZvYy0Ms51zezw0xaov+95cv5asPM\n/g78K84m+XzuEjk+8HDusrk4lwHPOudec86NyuI4/NYP+KTVz59GfpcP+prZ6sjz1UCs/zHy6dwl\ncj6ibZMvnc+JHJ8Bx0RSEk875/bP2OjSL5/PXSI8nbukyhYTaThKwLFmtso5tzNQ75xbGfm2yiof\nji2nrzLHOb6JrX8wM4vTQ5CT5y6GRM9H+1lQTp/HVhIZ5+vAnma2zjl3MvAE4dRhUOTruUuEp3OX\nVEA3s/IkB9d6H6sif37lnJtD+J+OWQ8KPhzbZ8CerX7ek/CsISfEO77IxZldzewL59xuwJcx9pGT\n5y6GRM5H+232iPwuH3R6fGb271bPn3HO3e2cKzGzRvJfPp+7Tnk9d+lOuUTN/Tjnejjnto887wkM\nBmJexc5RsfJarwE/dM7t45zrBpwBzM3csFIyFxgReT6C8GygjTw8d4mcj7nA+QDOuaOAr1ulnnJd\np8fnnOvrnHOR50cQLlcOQjCH/D53nfJ87tJw1XYo4ZxWE/AF8Ezk97sDT0We/4Dw1fjFwHJgQrav\nNvt1bJGfTwbeIlx9kBfHFhl3CfAs4eWQ64BeQTh30c4HMBoY3WqbOyOvLyFOdVYuPjo7PmBM5Fwt\nBv4JHJXtMXs4tseBz4ENkf/3Rgbs3MU9Pq/nTo1FIiIBoVvQiYgEhAK6iEhAKKCLiASEArqISEAo\noIuIBIQCuohIQCigi4gEhAK6iEhA/H8HLashiHbumgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x29146588>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print w.get_value()  # 接近 2\n",
    "print b.get_value()  # 接近 1\n",
    "\n",
    "plt.scatter(train_X, train_Y)\n",
    "plt.plot(train_X, w.get_value() * train_X + b.get_value(), 'r')\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
